package com.graduate.examapp.view.customview

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.Toast
import com.graduate.examapp.R
import com.graduate.examapp.entity.ExamRangeParam
import com.graduate.examapp.entity.LoginData
import com.graduate.examapp.entity.UserInfoFactory


class CustomFormDialog(
    mContext: Context,
    val agreeCallBack: (ExamRangeParam) -> Unit
) : Dialog(mContext) {
    private var disAgree: TextView? = null
    private var agree: TextView? = null
    private var invigilatorIdEt: CustomNormalEditText? = null
    private var invigilatorNameEt: CustomNormalEditText? = null
    private var courseNameEt: CustomNormalEditText? = null
    private var examPlaceEt: CustomNormalEditText? = null
    private var weekPicker: NumberPicker? = null
    private var dayPicker: NumberPicker? = null
    private var durationPicker: NumberPicker? = null
    private var insNameTx:TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        val inflate = View.inflate(context, R.layout.range_dialog, null)
        //this.window?.setBackgroundDrawable(context.getDrawable(R.drawable.dialog_back))
        this.setCanceledOnTouchOutside(false)
        this.setContentView(inflate)
        super.onCreate(savedInstanceState)
        initElement()
        initListener()
        //设置自适应的方法：
        //设置自适应的方法：
        val dialogParams: WindowManager.LayoutParams? = this.window?.attributes
        dialogParams?.width = WindowManager.LayoutParams.MATCH_PARENT
        dialogParams?.height = WindowManager.LayoutParams.WRAP_CONTENT
    }

    private fun initElement() {
        insNameTx = findViewById(R.id.insname_tx)
        insNameTx?.text = UserInfoFactory.loginData?.insName
        disAgree = findViewById(R.id.btn_cancel)
        agree = findViewById(R.id.btn_submit)
        invigilatorIdEt = findViewById(R.id.et_invigilator_id)
        invigilatorNameEt = findViewById(R.id.et_invigilator_name)
        examPlaceEt = findViewById(R.id.et_exam_place)
        courseNameEt = findViewById(R.id.et_course_name)

        weekPicker = findViewById(R.id.week_pick)
        dayPicker = findViewById(R.id.day_pick)
        durationPicker = findViewById(R.id.duration_pick)

        weekPicker?.let {
            it.minValue = 1
            it.maxValue = 25
        }

        dayPicker?.let {
            it.displayedValues = arrayOf("一", "二", "三", "四", "五", "六", "天")
            it.minValue = 1
            it.maxValue = 7
        }

        durationPicker?.let {
            it.displayedValues = arrayOf(
                "10:00-12:00",
                "12:00-14:00",
                "16:00-18:00",
                "18:00-20:00",
                "20:00-22:00",
            )
            it.minValue = 1
            it.maxValue = 5
        }
    }

    private fun initListener() {
        disAgree?.setOnClickListener {
            this.cancel()
        }

        agree?.setOnClickListener {
            this.cancel()
            if (
                !invigilatorIdEt?.editText?.text.toString().isNullOrBlank() &&
                !invigilatorNameEt?.editText?.text.toString().isNullOrBlank() &&
                !examPlaceEt?.editText?.text.toString().isNullOrBlank() &&
                weekPicker?.value != null &&
                dayPicker?.value != null &&
                !UserInfoFactory.loginData?.insName.toString().isNullOrBlank() &&
                //30,
                !courseNameEt?.editText?.text.toString().isNullOrBlank() &&
                !UserInfoFactory.xnxqh.toString().isNullOrBlank() &&
                durationPicker?.value != null
            ) {
                val examRangeParam = ExamRangeParam(
                    invigilatorIdEt?.editText?.text.toString(),
                    invigilatorNameEt?.editText?.text.toString(),
                    examPlaceEt?.editText?.text.toString(),
                    weekPicker?.value!!,
                    dayPicker?.value!!,
                    UserInfoFactory.loginData?.insName.toString(),
                    //30,
                    courseNameEt?.editText?.text.toString(),
                    UserInfoFactory.xnxqh.toString(),
                    durationPicker?.value!!
                )
                Log.d("CustomFormDialog", examRangeParam.toString())
                agreeCallBack(examRangeParam)
            } else {
                Toast.makeText(context, "请输入完成参数", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

